home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 February / PC Plus Super CD (Issue 124) (PCP124-2-97) (February 1997).iso / ms / vb5cce / controls / samples / clrbox2 / clrbox2.exe / ClrBox2.ctl < prev    next >
Encoding:
Text File  |  1996-10-25  |  8.7 KB  |  253 lines

  1. VERSION 5.00
  2. Begin VB.UserControl ColorBox2 
  3.    Appearance      =   0  'Flat
  4.    AutoRedraw      =   -1  'True
  5.    BackColor       =   &H8000000A&
  6.    ClientHeight    =   3600
  7.    ClientLeft      =   0
  8.    ClientTop       =   0
  9.    ClientWidth     =   4800
  10.    PropertyPages   =   "ClrBox2.ctx":0000
  11.    ScaleHeight     =   3600
  12.    ScaleWidth      =   4800
  13.    ToolboxBitmap   =   "ClrBox2.ctx":0004
  14.    Begin VB.Shape shpBox 
  15.       BorderColor     =   &H00FF8000&
  16.       Height          =   3375
  17.       Left            =   120
  18.       Top             =   120
  19.       Width           =   4575
  20.    End
  21. End
  22. Attribute VB_Name = "ColorBox2"
  23. Attribute VB_GlobalNameSpace = False
  24. Attribute VB_Creatable = True
  25. Attribute VB_PredeclaredId = False
  26. Attribute VB_Exposed = True
  27. 'This version of ColorBox uses a timer class written
  28. 'by Mike Wills.  It allows you to create more instances
  29. 'of ColorBox than a timer control does.
  30.  
  31. Option Explicit
  32.  
  33. Private WithEvents timBoxMove As clsTimer 'The timer class
  34. Attribute timBoxMove.VB_VarHelpID = -1
  35.  
  36. Private boolWiden As Boolean 'Are we expanding or contracting?
  37. Private boolLengthen As Boolean 'Are we expanding or contracting?
  38. Private intOrigWidth As Integer 'Original width of box
  39. Private intOrigHeight As Integer 'Original height of box
  40.  
  41. Private Const RedOffset As Long = &H1 'Value used to extract Red portion of Color value
  42. Private Const GreenOffset As Long = &H100 'Value used to extract Green portion of Color value
  43. Private Const BlueOffset As Long = &H10000 'Value used to extract Blue portion of Color value
  44.  
  45. Private Const RedMax As Byte = 255 'The maximum red value
  46. Private Const RedMin As Byte = 0 'The minimum red value
  47. Private Const GreenMax As Byte = 255 'The maximum green value
  48. Private Const GreenMin As Byte = 0 'The minimum green value
  49. Private Const BlueMax As Byte = 255 'The maximum blue value
  50. Private Const BlueMin As Byte = 0 'The minimum blue value
  51.  
  52. 'Color change increments
  53. Private Const ColorIncr As Integer = 1
  54. Private Const ColorDecr As Integer = -1
  55.  
  56. 'Margin widths between the edge of the usercontrol and the box.
  57. Private Const LeftMargin As Single = 5
  58. Private Const TopMargin As Single = 5
  59. Private Const RightMargin As Single = 5
  60. Private Const BottomMargin As Single = 5
  61.  
  62. Event BoxColorChange(NewColor As Long) 'Event that fires when the box color changes
  63.                                        
  64. 'Writing the BoxColor Property.
  65. Public Property Let BoxColor(Color As OLE_COLOR)
  66.     shpBox.BorderColor = Color
  67. End Property
  68.  
  69. 'Reading the BoxColor Property.
  70. Public Property Get BoxColor() As OLE_COLOR
  71.     BoxColor = shpBox.BorderColor
  72. End Property
  73.  
  74. 'Writing the Enabled Property.
  75. Public Property Let Enabled(ByVal AreWeOn As Boolean)
  76.     timBoxMove.Enabled = AreWeOn
  77. End Property
  78.  
  79. 'Reading the Enabled Property.
  80. Public Property Get Enabled() As Boolean
  81.     Enabled = timBoxMove.Enabled
  82. End Property
  83.  
  84. 'Writing the Interval Property.
  85. Public Property Let Interval(ByVal HowFast As Integer)
  86.     timBoxMove.Interval = HowFast
  87. End Property
  88.  
  89. 'Reading the Interval Property.
  90. Public Property Get Interval() As Integer
  91.     Interval = timBoxMove.Interval
  92. End Property
  93.  
  94. Private Sub UserControl_AmbientChanged(PropertyName As String)
  95.     If PropertyName = "BackColor" Then BackColor = Ambient.BackColor
  96. End Sub
  97.  
  98. Private Sub UserControl_InitProperties()
  99.     'Initializing BoxColor (Default = A nice shade of blue)
  100.     BoxColor = RGB(0, 128, 256)
  101.     'Initializing Enabled (Default = False)
  102.     Enabled = False
  103.     'Initializing Interval (Default = 50 milliseconds)
  104.     Interval = 50
  105. End Sub
  106.  
  107. 'This loads the current values of your persistent properties
  108. 'If a value has not been set, it loads a default value
  109. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  110.     'Loading BoxColor (Default = A nice shade of blue)
  111.     BoxColor = PropBag.ReadProperty("BoxColor", 16744448)
  112.     'Loading Enabled (Default = False)
  113.     Enabled = PropBag.ReadProperty("Enabled", False)
  114.     'Loading Interval (Default = 50 milliseconds)
  115.     Interval = PropBag.ReadProperty("Interval", 50)
  116. End Sub
  117.  
  118. 'This saves the value of your persistent properties if you change them
  119. 'in design mode
  120. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  121.     'Saving BoxColor
  122.     PropBag.WriteProperty "BoxColor", BoxColor
  123.     'Saving Enabled
  124.     PropBag.WriteProperty "Enabled", Enabled
  125.     'Saving Interval
  126.     PropBag.WriteProperty "Interval", Interval
  127. End Sub
  128.  
  129. 'This routine changes the size of the box and changes the color.  Red, Green
  130. 'and Blue values are calculated independently.  They cycle between 0 and 255
  131. 'starting with the initial value of BoxColor.
  132. Private Sub timBoxMove_Timer()
  133.     Dim I            As Integer
  134.     Dim wthBox       As Single 'Width of box
  135.     Dim hgtBox       As Single 'Height of box
  136.     Dim lftBox       As Single 'Left border of box
  137.     Dim topBox       As Single 'Top border
  138.     
  139.     Dim clrBorderCol As Long 'The full 24 bit bordercolor
  140.     Dim cRed         As Byte 'The red portion of clrBorderCol
  141.     Dim cGreen       As Byte 'The green portion of clrBorderCol
  142.     Dim cBlue        As Byte 'The blue portion of clrBorderCol
  143.     
  144.     Static iRed      As Integer 'The value added to the red portion of clrBorderCol
  145.     Static iGreen    As Integer 'The value added to the green portion of clrBorderCol
  146.     Static iBlue     As Integer 'The value added to the blue portion of clrBorderCol
  147.     
  148.  
  149.     With shpBox
  150.         wthBox = .Width
  151.         hgtBox = .Height
  152.         lftBox = .Left
  153.         topBox = .Top
  154.         clrBorderCol = .BorderColor
  155.         cRed = ((clrBorderCol \ RedOffset) And &HFF) 'Extract red portion of BorderColor
  156.         cGreen = ((clrBorderCol \ GreenOffset) And &HFF) 'Extract green portion of BorderColor
  157.         cBlue = ((clrBorderCol \ BlueOffset) And &HFF) 'Extract blue portion of BorderColor
  158.         
  159.         For I = 1 To 2
  160.         
  161.             If wthBox < 5 And boolWiden = False Then boolWiden = True
  162.             If ((wthBox + 5) >= intOrigWidth Or lftBox < 5) And _
  163.                 boolWiden = True Then boolWiden = False
  164.             If hgtBox < 5 And boolLengthen = False Then boolLengthen = True
  165.             If ((hgtBox + 5) >= intOrigHeight Or topBox < 5) And _
  166.                 boolLengthen = True Then boolLengthen = False
  167.                 
  168.             If boolWiden = False Then
  169.                 lftBox = lftBox + 1
  170.                 wthBox = wthBox - 2
  171.             Else
  172.                 lftBox = lftBox - 1
  173.                 wthBox = wthBox + 2
  174.             End If
  175.             If boolLengthen = False Then
  176.                 topBox = topBox + 1
  177.                 hgtBox = hgtBox - 2
  178.             Else
  179.                 topBox = topBox - 1
  180.                 hgtBox = hgtBox + 2
  181.             End If
  182.             
  183.             .Move lftBox, topBox, wthBox, hgtBox
  184.         Next I
  185.         
  186.         
  187.         
  188.         If cRed = RedMax Then
  189.             iRed = ColorDecr
  190.         Else
  191.             If cRed = RedMin Then
  192.                 iRed = ColorIncr
  193.             Else
  194.                 If iRed = 0 Then
  195.                     If cRed < 255 Then iRed = ColorIncr Else iRed = ColorDecr
  196.                 End If
  197.             End If
  198.         End If
  199.         
  200.         If cGreen = GreenMax Then
  201.             iGreen = ColorDecr
  202.         Else
  203.             If cGreen = GreenMin Then
  204.                 iGreen = ColorIncr
  205.             Else
  206.                 If iGreen = 0 Then
  207.                     If cGreen < GreenMax Then iGreen = ColorIncr Else iGreen = ColorDecr
  208.                 End If
  209.             End If
  210.         End If
  211.         
  212.         If cBlue = BlueMax Then
  213.             iBlue = ColorDecr
  214.         Else
  215.             If cBlue = BlueMin Then
  216.                 iBlue = ColorIncr
  217.             Else
  218.                 If iBlue = 0 Then
  219.                     If cBlue < BlueMax Then iBlue = ColorIncr Else iBlue = ColorDecr
  220.                 End If
  221.             End If
  222.         End If
  223.         
  224.         .BorderColor = RGB(cRed + iRed, cGreen + iGreen, cBlue + iBlue)
  225.         RaiseEvent BoxColorChange(.BorderColor)
  226.     End With
  227. End Sub
  228.  
  229. Private Sub UserControl_Initialize()
  230.     ScaleMode = vbPixels 'Pixels
  231.     intOrigWidth = shpBox.Width 'Record original width
  232.     intOrigHeight = shpBox.Height 'Record original height
  233.     
  234.     Set timBoxMove = New clsTimer 'instanciate timer class
  235. End Sub
  236.  
  237. Private Sub UserControl_Resize()
  238.     'Reset box shape
  239.     shpBox.Move LeftMargin, TopMargin, Abs((Width / Screen.TwipsPerPixelX) - RightMargin), Abs((Height / Screen.TwipsPerPixelY) - BottomMargin)
  240. End Sub
  241.  
  242. Private Sub UserControl_Show()
  243.     BackColor = Ambient.BackColor
  244. End Sub
  245.  
  246. Private Sub UserControl_Terminate()
  247.     If Not timBoxMove Is Nothing Then
  248.         If timBoxMove.Enabled Then timBoxMove.Enabled = False
  249.         Set timBoxMove = Nothing
  250.     End If
  251. End Sub
  252.  
  253.